#include <iostream>
#include <future>
#include <thread>
#include <unistd.h>

void myFunction(std::promise<int> prom)
{
    // 模拟工作
    sleep(1);
    prom.set_value(42);
}

void threadFunction(std::shared_future<int> fut)
{
    try
    {
        int result = fut.get();
        std::cout << "Result: " << result << std::endl;
    }
    catch (const std::future_error &e)
    {
        std::cout << "Future error: " << e.what() << std::endl;
    }
}

void use_shared_future()
{
    std::promise<int> promise;
    std::shared_future<int> future = promise.get_future();

    std::thread myThread1(myFunction, std::move(promise));

    // 使用 share() 方法获取新的 shared_future 对象

    std::thread myThread2(threadFunction, future);

    std::thread myThread3(threadFunction, future);

    myThread1.join();
    myThread2.join();
    myThread3.join();
}

int main()
{
}